package org.sgx.gwteditors.client.impl1;

import java.util.Date;
import java.util.List;

import org.sgx.gwteditors.client.editor.Editor;
import org.sgx.gwteditors.client.editor.EditorCreator;
import org.sgx.gwteditors.client.editor.EditorFramework;
import org.sgx.gwteditors.client.editor.Types;
import org.sgx.gwteditors.client.editor.props.PropertyHaver;
import org.sgx.gwteditors.client.impl1.basetypes.Color;
import org.sgx.gwteditors.client.impl1.basetypes.InRangeDouble;
import org.sgx.gwteditors.client.impl1.basetypes.InRangeInteger;
import org.sgx.gwteditors.client.impl1.basetypes.SelectionList;
import org.sgx.gwteditors.client.impl1.basetypes.Time;
import org.sgx.gwteditors.client.impl1.complex.Array1DEditor3;
import org.sgx.gwteditors.client.impl1.complex.PropertyHaverEditor2;
import org.sgx.gwteditors.client.impl1.complex.SelectionListSingleSelEditor;
import org.sgx.gwteditors.client.impl1.edbean.UseEditor;
import org.sgx.gwteditors.client.impl1.misc.ImageUrlEditor;
import org.sgx.gwteditors.client.impl1.misc.LinkEditor;
import org.sgx.gwteditors.client.impl1.misc.PasswordEditor;
import org.sgx.gwteditors.client.impl1.simple.BooleanEditor;
import org.sgx.gwteditors.client.impl1.simple.ColorEditor;
import org.sgx.gwteditors.client.impl1.simple.DateEditor;
import org.sgx.gwteditors.client.impl1.simple.DoubleEditorEntry;
import org.sgx.gwteditors.client.impl1.simple.InRangeDoubleEditor;
import org.sgx.gwteditors.client.impl1.simple.InRangeIntegerEditor;
import org.sgx.gwteditors.client.impl1.simple.IntegerEditorEntry;
import org.sgx.gwteditors.client.impl1.simple.IntegerEditorEntry2;
import org.sgx.gwteditors.client.impl1.simple.StringEditorEntry;
import org.sgx.gwteditors.client.impl1.simple.TimeEditor;
import org.sgx.gwteditors.client.impl1.simple.TimeJavaDateEditor;
import org.sgx.gwteditors.client.util.Util;
/**
 * this is the (only) class resposible of editor registration and editor type definitions of implementation "impl1". 
 * @author sg
 *
 */
public class EditorFramework1 implements EditorFramework {
	
	//TODO: move these constants to EditorFramework
	public static final String 
		TYPE_INTEGER = "integer", 
		TYPE_DOUBLE = "double",  
		TYPE_DATE="date", 
		TYPE_DATETIME="datetime", 
		TYPE_BOOLEAN="boolean", 
		TYPE_STRING="string",
		TYPE_LISTSINGLE="list single",
		TYPE_LISTMULTIPLE="list multple",
		TYPE_PROPERTYHAVER="property haver",
		TYPE_PROPERTYHAVERLIST="property haver list", //java.util.List<? extends PropertyHaver> 
		TYPE_COLOR = "color", 
		TYPE_TIME="time",
		TYPE_INRANGEDOUBLE = "double in range", 
		TYPE_INRANGEINTEGER = "integer in range";
	
	public static final String[] ALL_EDITOR_TYPES = {
		TYPE_INTEGER, TYPE_DOUBLE, 
		TYPE_DATE, TYPE_DATETIME, TYPE_BOOLEAN, 
		TYPE_STRING, TYPE_LISTSINGLE, TYPE_LISTMULTIPLE, 
		TYPE_PROPERTYHAVER, TYPE_COLOR, TYPE_TIME,
		TYPE_INRANGEINTEGER, TYPE_INRANGEDOUBLE
	};

	private boolean started=false;

	public void restart() {
		started=false; 
		start(); 
	}
	
	@Override
	public void start() {
		
		if(started)
			return; 
		
		started=true; 
		
		loadEditors();		
	}

	protected void loadEditors() {
		Types types = Types.getInstance();
		
		types.registerEditor(TYPE_BOOLEAN, Boolean.class, new EditorCreator<Boolean>() {
			@Override
			public Editor<Boolean> create() {
				return new BooleanEditor();
			}
		});		
		types.registerEditor(TYPE_INTEGER, Integer.class, new EditorCreator<Integer>() {
			@Override
			public Editor<Integer> create() {
				return new IntegerEditorEntry2();
			}
		});
		types.registerEditor(TYPE_INTEGER, Integer.class, new EditorCreator<Integer>() {
			@Override
			public Editor<Integer> create() {
				return new IntegerEditorEntry();
			}
		});
		types.registerEditor(TYPE_DOUBLE, Integer.class, new EditorCreator<Double>() {
			@Override
			public Editor<Double> create() {
				return new DoubleEditorEntry();
			}
		});		
		types.registerEditor(TYPE_STRING, String.class, new EditorCreator<String>() {
			@Override
			public Editor<String> create() {
				return new StringEditorEntry();
			}
		});		
		types.registerEditor(TYPE_STRING, String.class, new EditorCreator<String>() {
			@Override
			public Editor<String> create() {
				return new org.sgx.gwteditors.client.impl1.misc.RichTextEditor();
			}
		});		
		types.registerEditor(TYPE_STRING, String.class, new EditorCreator<String>() {
			@Override
			public Editor<String> create() {
				return new LinkEditor();
			}
		});		
		types.registerEditor(TYPE_STRING, String.class, new EditorCreator<String>() {
			@Override
			public Editor<String> create() {
				return new ImageUrlEditor();
			}
		});		
		types.registerEditor(TYPE_STRING, String.class, new EditorCreator<String>() {
			@Override
			public Editor<String> create() {
				return new PasswordEditor();
			}
		});				
		types.registerEditor(TYPE_BOOLEAN, Boolean.class, new EditorCreator<Boolean>() {
			@Override
			public Editor<Boolean> create() {
				return new BooleanEditor();
			}
		});		
		types.registerEditor(TYPE_DATE, Date.class, new EditorCreator<Date>() {
			@Override
			public Editor<Date> create() {
				return new DateEditor();
			}
		});
		types.registerEditor(TYPE_DATE, Date.class, new EditorCreator<Date>() {
			@Override
			public Editor<Date> create() {
				return new TimeJavaDateEditor();
			}
		});		
		types.registerEditor(TYPE_TIME, Time.class, new EditorCreator<Time>() {
			@Override
			public Editor<Time> create() {
				return new TimeEditor();
			}
		});
		types.registerEditor(TYPE_DATETIME, Date.class, new EditorCreator<Date>() {
			@Override
			public Editor<Date> create() {
				return new DateEditor();
			}
		});
		types.registerEditor(TYPE_LISTSINGLE, SelectionList.class, new EditorCreator<SelectionList<Object>>() {
			@Override
			public Editor<SelectionList<Object>> create() {
				return new SelectionListSingleSelEditor<Object>();
			}
		});
		types.registerEditor(TYPE_COLOR, Color.class, new EditorCreator<Color>() {
			@Override
			public Editor<Color> create() {
				return new ColorEditor();
			}
		});
		types.registerEditor(TYPE_INRANGEINTEGER, InRangeInteger.class, new EditorCreator<InRangeInteger>() {
			@Override
			public Editor<InRangeInteger> create() {
				return new InRangeIntegerEditor();
			}
		});
		types.registerEditor(TYPE_INRANGEDOUBLE, InRangeDouble.class, new EditorCreator<InRangeDouble>() {
			@Override
			public Editor<InRangeDouble> create() {
				return new InRangeDoubleEditor();
			}
		});
		types.registerEditor(TYPE_PROPERTYHAVER, PropertyHaver.class, new EditorCreator<PropertyHaver>() {
			@Override
			public Editor<PropertyHaver> create() {
				return new PropertyHaverEditor2();
			}
		});
		types.registerEditor(TYPE_PROPERTYHAVERLIST, List.class, new EditorCreator<List<? extends PropertyHaver>>() {
			@Override
			public Editor<List<? extends PropertyHaver>> create() {
				return new Array1DEditor3(TYPE_PROPERTYHAVER, null, null);
			}
		});
	}

	public static boolean isValidEditorName(String editorName) {
		return Util.contains(ALL_EDITOR_TYPES, editorName);
	}

}
